package com.kapcb.ruleengine.core;

import com.kapcb.ruleengine.api.Action;
import com.kapcb.ruleengine.api.Condition;
import com.kapcb.ruleengine.api.Facts;

import java.util.ArrayList;
import java.util.List;

/**
 * <a>Title: DefaultRule </a>
 * <a>Author: Kapcb <a>
 * <a>Description: DefaultRule <a>
 *
 * @author Kapcb
 * @version 1.0
 * @date 2024/8/11 10:31
 * @since 1.0
 */
public class DefaultRule extends AbstractRule {

    private final Condition condition;

    private final List<Action> actions;

    public DefaultRule(Condition condition, Action action) {
        this.condition = condition;
        this.actions = new ArrayList<>();
        this.actions.add(action);
    }

    public DefaultRule(Condition condition, List<Action> actions) {
        this.condition = condition;
        this.actions = actions;
    }

    public DefaultRule(String name, String description, int priority, Condition condition, List<Action> actions) {
        super(name, description, priority);
        this.condition = condition;
        this.actions = actions;
    }

    @Override
    public boolean evaluate(Facts facts) {
        return condition.evaluate(facts);
    }

    @Override
    public void execute(Facts facts) {
        for (Action action : actions) {
            action.execute(facts);
        }
    }

    @Override
    public boolean apply(Facts facts) {
        if (condition.evaluate(facts)) {
            for (Action action : actions) {
                action.execute(facts);
            }
            return true;
        }
        return false;
    }
}
